package com.cxh.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.common.dao.BaseDao;
import com.common.entity.A_record;
import com.cxh.dao.A_recordDao;

public class A_recordDaoImpl extends BaseDao<A_record> implements A_recordDao {

	@Override
	/**
	 * ע�⣺ʹ��ʱmp��key����Ϊ���������
	 * ����Ϊ������Բ����������
	 * Integer record_id, Integer auctionMsg_id, Integer customer_id,
	   Double money, 	  Date create_date
	 */
	public List<A_record> findByCondition(Map<String, Object> mp) {
		List<Object> pList = new ArrayList<Object>();
		StringBuffer sb = new StringBuffer(" select * from a_record where 1=1 ");
		if(!(mp==null||mp.size()<1))
			for(Entry<String, Object> et:mp.entrySet()){
				if(et.getKey()=="create_date")
					sb.append("and to_char(create_date,'yyyy-MM-dd')=? ");
				else
					sb.append("and "+et.getKey()+" = ? ");
				pList.add(et.getValue());
			}
		return findBySql(sb.toString(), pList.toArray());
	}

	@Override
	public A_record findById(int id) {
		String sql = " select * from a_record where record_id = ? ";
		Object[] obj = {id};
		List<A_record> lg = findBySql(sql, obj);
		return (lg!=null&&lg.size()>0)?lg.get(0):null;
	}

	@Override
	/**
	 * ע�⣺ʹ��ʱmp��key����Ϊ���������
	 * ����Ϊ������Բ����������
	 * Integer record_id, Integer auctionMsg_id, Integer customer_id,
	   Double money, 	  Date create_date
	 */
	public int updateA_record(Map<String, Object> mp) {
		List<Object> pList = new ArrayList<Object>();
		StringBuffer sb = new StringBuffer(" update a_record set ");
		if(mp!=null&&mp.size()>0){
			for(Entry<String, Object> et:mp.entrySet()){
				if(!(et.getKey()=="record_id")){
					sb.append(et.getKey()+" = ?,");
					pList.add(et.getValue());
				}
			}
			String sb1 = sb.toString();
			sb1=sb1.substring(0, sb1.length()-1);
			sb = new StringBuffer(sb1);
			for(Entry<String, Object> et:mp.entrySet()){
				if(et.getKey()=="record_id"){
					sb.append("where record_id = ?");
					pList.add(et.getValue());
				}
			}
		}
		return executeUpdate(sb.toString(), pList.toArray());
	}

	@Override
	public int deleteA_record(int id) {
		String sql = "delete from a_record where record_id = ?";
		Object[] obj = {id};
		return executeUpdate(sql, obj);
	}

	@Override
	/**
	 * ע�⣺ʹ��ʱmp��key����Ϊ���������
	 * ����Ϊ������Բ����������
	 * Integer record_id, Integer auctionMsg_id, Integer customer_id,
	   Double money, 	  Date create_date
	 */
	public int addA_record(Map<String, Object> mp) {
		String sql = "insert into A_record values(SEQ_A_record.nextval,?,?,?,?)";
		Object[] objs = {mp.get("auctionMsg_id"),mp.get("customer_id"),mp.get("money"),mp.get("create_date")};
		return executeUpdate(sql, objs);
	}

	@Override
	public A_record getEntity(ResultSet rs) {
		A_record r = new A_record();
		try {
			r.setAuctionMsg_id(rs.getInt("auctionMsg_id"));
			r.setCreate_date(rs.getDate("create_date"));
			r.setCustomer_id(rs.getInt("customer_id"));
			r.setMoney(rs.getDouble("money"));
			r.setRecord_id(rs.getInt("record_id"));
			return r;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

}
